package com.dotc.kotlinmvvmdemo.activity

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.dotc.kotlinmvvmdemo.R
import com.dotc.kotlinmvvmdemo.databinding.ActivityMainBinding
import com.dotc.kotlinmvvmdemo.utils.EventHandlers
import com.dotc.kotlinmvvmdemo.viewModel.MainViewModel
import java.util.*

class MainActivity : AppCompatActivity() {
    private val viewModel: MainViewModel by lazy { ViewModelProvider(
        this, ViewModelProvider.AndroidViewModelFactory(
            application
        )
    ).get(MainViewModel::class.java) }

    @Override
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = DataBindingUtil.setContentView<ActivityMainBinding>(
            this@MainActivity,
            R.layout.activity_main
        )
        //绑定生命周期
        binding.lifecycleOwner = this
        //绑定viewModel
        binding.viewModel = viewModel


        //让其它对象绑定MainActivity
        val handlers = EventHandlers()
        binding.handler = handlers
        binding.activity = TwoActivity()

        //调用viewModel方法
        viewModel.context = this
        viewModel.updateTime()

    }


}


